package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasOrderTypeHotelTime;
import com.kingint.mbg.bas.entity.BasWorkOrderType;

import com.kingint.mbg.bas.mapper.BasOrderTypeHotelTimeMapper;
import com.kingint.web.admin.base.service.BasOrderTypeHotelTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.admin.base.service.BasWorkOrderTypeService;
import com.kingint.web.sys.service.RedisService;
import com.kingint.web.util.ReportToType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * <p>
 * 酒店自定义上报时间表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2021-01-15
 */
@Service
public class BasOrderTypeHotelTimeServiceImpl extends ServiceImpl<BasOrderTypeHotelTimeMapper, BasOrderTypeHotelTime> implements BasOrderTypeHotelTimeService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private BasWorkOrderTypeService workOrderTypeService;

    private static Map<String,Long> timeMap = new HashMap<>(); //默认值
    private static Map<String,Long> hotelTimeMap = new HashMap<>();  //酒店自定义值

    @PostConstruct
    private void setmap(){
        List<BasWorkOrderType> list = workOrderTypeService.list(null);
        list.forEach(v->{
            timeMap.put(v.getName()+ ReportToType.ReportDirector,v.getReportDirectorTime()*60*1000l); //上报主管
            timeMap.put(v.getName()+ReportToType.ReportManager,v.getReportManagerTime()*60*1000l); //上报经理
            timeMap.put(v.getName()+ReportToType.ReportTop,v.getReportTopTime()*60*1000l); //上报高层
        });

        List<BasOrderTypeHotelTime> list1 = this.list(null);
        list1.forEach(v->{
            hotelTimeMap.put(v.getHotelId()+v.getWorkTypeName()+ReportToType.ReportDirector,v.getReportDirectorTime()*60*1000l); //上报主管
            hotelTimeMap.put(v.getHotelId()+v.getWorkTypeName()+ReportToType.ReportManager,v.getReportManagerTime()*60*1000l); //上报经理
            hotelTimeMap.put(v.getHotelId()+v.getWorkTypeName()+ReportToType.ReportTop,v.getReportTopTime()*60*1000l); //上报高层
        });

    }




    /**
     * 定时查询工单情况并上报更换处理人
     * 存入字段  使用hash来存储  key1：hotelktneed  key2：酒店ID_workId_workType  value：时间戳
     * 方便更换接单人之后时间戳重新计时
     */
    @Override
    public void tim() {

        List<String> hotelktneed = redisService.hvalues("hotelktneed");

        for (String hotel :hotelktneed) {
            Long time = null; //设定时间
            String[] s = hotel.split("_"); //分割获取的字段，取出信息
            Integer hotelId = Integer.parseInt(s[0]); //酒店id
            Integer workId = Integer.parseInt(s[1]); //总工单id
            String workType = s[2]; //工单类型
            Long timeStamp = Long.parseLong(redisService.hget("hotelktneed", hotel)); //创建时间

            Long interval = new Date().getTime() - timeStamp; //创建时间和当前时间间隔

            QueryWrapper<BasOrderTypeHotelTime> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("hotel_id",s[0]);
            BasOrderTypeHotelTime one = this.getOne(queryWrapper);
            if (Objects.isNull(one)) { //使用默认值
//                timeMap.get(workType+)
                hotelTimeMap.put(hotelId+workType+ReportToType.ReportDirector,timeMap.get(workType+ReportToType.ReportDirector));
                hotelTimeMap.put(hotelId+workType+ReportToType.ReportManager,timeMap.get(workType+ReportToType.ReportManager));
                hotelTimeMap.put(hotelId+workType+ReportToType.ReportTop,timeMap.get(workType+ReportToType.ReportTop));
            }

            int i = toCompare(interval, hotelTimeMap.get(hotelId + workType + ReportToType.ReportDirector), hotelTimeMap.get(hotelId + workType + ReportToType.ReportManager), hotelTimeMap.get(hotelId + workType + ReportToType.ReportTop));

            switch (i){
                case 2:
                    // TODO: 2021-01-18 上报主管 
                    break;
                case 3:
                    // TODO: 2021-01-18 上报经理 
                    break;
                case 4:
                    // TODO: 2021-01-18 上报高层 
                    break;
            }


        }

    }

    /**
     * 工单定时上报写入
     * @param hotelId 酒店id
     * @param workId 总工单id
     * @param workType 工单类型
     * @param time 创建时间戳
     */
    @Override
    public void addtim(Integer hotelId, Integer workId, String workType, Long time) {
        redisService.hset("hotelktneed",hotelId+"_"+workId+"_"+workType,time.toString());
    }

    @Override
    public void deltim(Integer hotelId, Integer workId, String workType) {
        redisService.hremove("hotelktneed",hotelId+"_"+workId+"_"+workType);
    }


    //时间比较
    private int toCompare(Long time,Long time1 ,Long time2,Long time3){
        if (time <= time1){
            return 1;
        }else if (time1 < time && time <= time2){
            return 2;
        }else if (time2 < time && time <= time3){
            return 3;
        }else if (time >time3){
            return 4;
        }
        return 0;
    }

}
